/*
 * Created on 17-mar-2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package net.jforum.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.ThemeDAO;
import net.jforum.entities.Theme;
import net.jforum.util.preferences.SystemGlobals;

/**
 * @author mazzolini
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class GenericThemeDAO extends AutoKeys implements ThemeDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jforum.model.ThemeModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p;
		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ThemeModel.selectAll"));
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		return list;
	}

	/**
	 * @param rs
	 * @return
	 */
	protected List processSelectAll(ResultSet rs) throws Exception {
		List list = new ArrayList();
		while (rs.next()) {
			Theme t = new Theme();
			initTheme(t, rs);
			list.add(t);
		}

		return list;
	}

	protected void initTheme(Theme t, ResultSet rs) throws Exception {
		t.setThemesId(rs.getInt("THEMES_ID"));
		t.setStyleName(rs.getString("TEMPLATE_NAME"));
		t.setTemplateName(rs.getString("STYLE_NAME"));
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.jforum.model.ThemeModel#selectByID()
	 */
	public Theme selectByID(int themeID) throws Exception {
		PreparedStatement p;
		p = JForumExecutionContext.getConnection().prepareStatement(
				SystemGlobals.getSql("ThemeModel.selectById"));
		p.setInt(1, themeID);
		ResultSet rs = p.executeQuery();
		Theme t = new Theme();
		rs.next();
		initTheme(t, rs);
		rs.close();
		p.close();
		return t;
	}

}
